package TUI;

import ControllerLayer.*;
import ModelLayer.*;

import java.util.Scanner;

/**
 * Write a description of class ToolUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ToolUI
{
    private ToolCtr toolCtr;
    public ToolUI()
    {
        toolCtr = ToolCtr.getInstance();
    }
    
    public void start()
    {
        clr();
        toolMenu();
    }
    
    private void toolMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeToolMenu();
            switch(choise)
            {
                case 1:
                    createToolDescription();
                    break;
                case 2:
                    editToolDescription();
                    break;
                case 3:
                    showToolDescription();
                    break;
                case 4:
                    createTool();
                    break;
                case 5:
                    deleteTool();
                    break;
                case 6:
                    showToolList();
                    break;
                case 7:
                    exit = true;
                    break;
                default:
                    break;
            }
        }
    }
    
    private int writeToolMenu()
    {
        print(" *** Vaerktoej Menu *** ");
        print(" (1) Tilfoej ny vaerktoejstype");
        print(" (2) Rediger vaerktoejstype");
        print(" (3) Vis detaljer om vaerktoejstype");
        print(" (4) Tilfoej nyt vaerktoej");
        print(" (5) Fjern vaerktoej");
        print(" (6) Vis liste af vaerktoejstyper");
        print(" (7) Gaa tilbage til hovedmenu");
        print("\n Foretag din beslutning: ");
            
        boolean exit = false;
        int choise = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                choise = keyboard.nextInt();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt valg, proev igen");
            }
        }
        return choise;
    }
    
    private String getInputString(String str)
    {
        print(str);
        
        boolean exit = false;
        String string = "";
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                string = keyboard.nextLine();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return string;
    }
    
    private int getInputInt(String str)
    {
        print(str);
        
        boolean exit = false;
        int integer = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                integer = keyboard.nextInt();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return integer;
    }
    
    private double getInputDouble(String str)
    {
        print(str);
        
        boolean exit = false;
        double dob = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                dob = keyboard.nextDouble();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return dob;
    }
    
    private long getInputLong(String str)
    {
        print(str);
        
        boolean exit = false;
        long lon = 0;
        while(!exit)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                lon = keyboard.nextLong();
                exit = true;
            }
            catch(Exception e)
            {
                print("Ugyldigt input, proev igen");
            }
        }
        return lon;
    }
    
    private void printDescriptionDetails(ToolDescription description)
    {
        oneSpace();
        print("ID:          " + description.getDescriptionID());
        print("Beskrivelse: " + description.getDescription());
        print("Pris:        " + description.getPrice());
        oneSpace();        
        for(Tool tool : description.getToolList())
        {
            print(" Serienummer: " + tool.getSerialNumber());
        }
        oneSpace();
    }
    
    private void createToolDescription()
    {
        String description = getInputString("Indtast en beskrivelse af vaerktoejet:");
        double price = getInputDouble("Indtast prisen for at leje vaerktojet:");
        doubleSpace();
        
        toolCtr.createToolDescription(description,price);
    }
    
    private void editToolDescription()
    {
        long id = getInputLong("indtast vaerktoejstypens id:");
        String description = getInputString("Indtast vaerktoejets beskivelse:");
        double price = getInputDouble("Indtast vaerktoejets pris: ");
        doubleSpace();
        toolCtr.editToolDescription(id,description,price);
    }
    
    private void showToolDescription()
    {
        long id  = getInputLong("Indtast vaerktoejstypens id:");
        ToolDescription description = toolCtr.getToolDescription(id);
        printDescriptionDetails(description);
        oneSpace();
    }
    
    private void createTool()
    {
        long id = getInputLong("Indtast id nummeret for den vaerktoejstype som vaerktoejet skal tilknyttes:");
        long serial = getInputLong("Indtast serie nummeret paa vaerktoejet som skal oprettes:");
        doubleSpace();
        toolCtr.createTool(id,serial);
    }
    
    private void deleteTool()
    {
        long id = getInputLong("Indtast id nummeret paa den vaerktoejstype som vaerktoekjet er tilknytet: ");
        long serial = getInputLong("Indtast serie nummeret paa det vaerktoej som skal fjernes: ");
        doubleSpace();
        toolCtr.deleteTool(id,serial);
    }
    
    private void showToolList()
    {
        oneSpace();
        for(ToolDescription description : toolCtr.getToolDescriptionList())
        {
            print("ID: " + description.getDescriptionID() + " Description: " + description.getDescription());
        }
        oneSpace();
    }
    
    /**
     * This methid prints a sting to the terminal
     * 
     * @param   str A sting to be printed to the terminal.
     */
    private void print(String str)
    {
        System.out.println(str);
    }
    
    /**
     * This method clears the terminal.
     */
    private void clr()
    {
        print("\f");
    }
    
    private void oneSpace()
    {
        print("\n");
    }
    
    private void doubleSpace()
    {
       print("\n\n");
    }
    
}